package com.bigtotoro.widgets;

import android.content.Context;
import android.view.animation.Interpolator;
import android.widget.Scroller;

/**
 * 控制ViewPager的滑动速度
 *  try {
         Field mScroller;
         mScroller = ViewPager.class.getDeclaredField("mScroller");
         mScroller.setAccessible(true);
         FixedSpeedScroller scroller = new FixedSpeedScroller(mPager.getContext(), sInterpolator);
         scroller.setFixedDuration(5000);
         mScroller.set(mPager, scroller);
    } catch (NoSuchFieldException e) {
    } catch (IllegalArgumentException e) {
 } catch (IllegalAccessException e) {
    }
 */
public class FixedSpeedScroller extends Scroller {

    private int mDuration = 300;//设置移动速度

    public FixedSpeedScroller(Context context) {
        super(context);
    }

    public FixedSpeedScroller(Context context, Interpolator interpolator) {
        super(context, interpolator);
    }

    public FixedSpeedScroller(Context context, Interpolator interpolator, boolean flywheel) {
        super(context, interpolator, flywheel);
    }

    @Override
    public void startScroll(int startX, int startY, int dx, int dy, int duration) {
        // Ignore received duration, use fixed one instead
        super.startScroll(startX, startY, dx, dy, mDuration);
    }

    @Override
    public void startScroll(int startX, int startY, int dx, int dy) {
        // Ignore received duration, use fixed one instead
        super.startScroll(startX, startY, dx, dy, mDuration);
    }

    public void setDuration(int duration){
        mDuration = duration;
    }
}